package com.ruoyi.web.controller.system.app;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppPersonalCenter;
import com.ruoyi.system.service.IAppPersonalCenterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人应用Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Controller
@RequestMapping("/system/center")
public class AppPersonalCenterController extends BaseController
{
    private String prefix = "system/center";

    @Autowired
    private IAppPersonalCenterService appPersonalCenterService;

    /**
     * 查询个人应用列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppPersonalCenter appPersonalCenter)
    {
        startPage();
        List<AppPersonalCenter> list = appPersonalCenterService.selectAppPersonalCenterList(appPersonalCenter);
        return getDataTable(list);
    }

    /**
     * 导出个人应用列表
     */
    @Log(title = "个人应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppPersonalCenter appPersonalCenter)
    {
        List<AppPersonalCenter> list = appPersonalCenterService.selectAppPersonalCenterList(appPersonalCenter);
        ExcelUtil<AppPersonalCenter> util = new ExcelUtil<AppPersonalCenter>(AppPersonalCenter.class);
        return util.exportExcel(list, "个人应用数据");
    }

    /**
     * 新增保存个人应用
     */
    @Log(title = "个人应用", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppPersonalCenter appPersonalCenter) {
        AppPersonalCenter data = new AppPersonalCenter();
        data.setSiteId(appPersonalCenter.getSiteId());
        List<AppPersonalCenter> personalCenters = appPersonalCenterService.selectAppPersonalCenterList(data);
        if (personalCenters.size()!=0){
            return AjaxResult.error("该应用网址已存在，不能重复添加");
        }
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        appPersonalCenter.setAppSysUser(username);
        appPersonalCenter.setCreateTime(new Date(System.currentTimeMillis()));
        appPersonalCenter.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appPersonalCenterService.insertAppPersonalCenter(appPersonalCenter));
    }

    /**
     * 修改保存个人应用
     */
    @Log(title = "个人应用", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppPersonalCenter appPersonalCenter)
    {
        return toAjax(appPersonalCenterService.updateAppPersonalCenter(appPersonalCenter));
    }

    /**
     * 删除个人应用
     */
    @Log(title = "个人应用", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(appPersonalCenterService.deleteAppPersonalCenterByIds(ids));
    }
}
